/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.utils 
{
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class DateUtils
	{
		
		public static function isValidDay( day : String ) : Boolean
		{
			return Number(day) >= 1 && Number(day) <= 31 ;
		}
		
		public static function isValidMonth( month : String ) : Boolean
		{
			return Number(month) >= 1 && Number(month) <= 12 ;
		}
		
		public static function isValidYear( year : String ) : Boolean
		{
			return Number(year) >= 1900  &&   Number(year) < 2100;
		}
		
		public static function formatYYYYMMDD( day : String, month : String, year : String, separator : String = "-" ) : String
		{
			var d : Number = Number(day) ; 
			var m : Number = Number(month) ; 
			var y : Number = Number(year) ; 
			
			var ys : String = year ; 
			while ( ys.length < 4 ) ys = "0" + ys  ;
			
			return ys + separator +  ( m < 10 ? "0" + m.toString() : m.toString() ) + separator + ( d < 10 ? "0" + d.toString() : d.toString() )	;
		}
		
		/**
		 * return age in years from a date
		 * @param	date the date String in format "YYYY-MM-DD" 
		 * @return the age in years
		 */
		public static function getAge( date : String ) : Number 
		{
			var now : Date = new Date() ;
			
			var a : Array = date.split("-") ; 
			trace(a) ; 
			var birth : Date = new Date(a[0], a[1] -1, a[2]) ; 
			
			var years:Number = now.getFullYear() - birth.getFullYear();
			
			if (now.getMonth() < birth.getMonth()  || (now.getMonth() == birth.getMonth() && now.getDay() < birth.getDay())) years--;
			
			return years;		
		}
		
		public static function getMonthLabel( month : int, locale:  String = "fr" ) : String 
		{
			var monthes : Array ; 
			switch ( locale )
			{
				case Locale.FR : 
					monthes =  ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Décembre"] ;
				break  ; 
				case Locale.US :
				default : 
					monthes =  ["January", "February", "March", "April", "Mai", "June", "July", "August", "September", "October", "November", "December"] ;
				break  ;				
			}
			
			return monthes[(month - 1)] as String; 
		}
		
		public static function isOlderThan( date : String, min : Number = 18 ) : Boolean 
		{
			var cur : Date = new Date() ; 
			var a : Array = date.split("-") ; 
			var y : Number = Number(a[0]) ;
			var m : Number = Number(a[1]) ;
			var d : Number = Number(a[2]) ;
			
			if ( cur.getFullYear() - y > min )  return true ; 
			if ( cur.getFullYear() - y  < min )  return false ; 
			
			if ( (cur.getMonth() + 1) > m ) return true ; 
			if ( (cur.getMonth() + 1) < m ) return false ; 
			
			if ( cur.getDate() >= d ) return true ;
			else return false ;
		}
		
		public static function isYoungerThan( date : String, max : Number = 1000 ) :Boolean
		{
			var cur : Date = new Date() ; 
			var a : Array = date.split("-") ; 
			var y : Number = Number(a[0]) ;
			var m : Number = Number(a[1]) ;
			var d : Number = Number(a[2]) ;
			
			if ( cur.getFullYear() - y < max )  return true ; 
			if ( cur.getFullYear() - y  > max )  return false ; 
			
			if ( (cur.getMonth() + 1) < m ) return true ; 
			if ( (cur.getMonth() + 1) > m ) return false ; 
			
			if ( cur.getDate() < d ) return true ;
			else return false ;
		}
		
		
		
		static public function isValidDate( date:String ):Boolean
		{
			var maxJours:Array = new Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
			var a : Array = date.split("-") ; 
			var annee : Number = Number(a[0]) ;
			var mois : Number = Number(a[1]) ;
			var jour : Number = Number(a[2]) ;
			
			var current : Date = new Date() ; 
			
			if (isNaN(jour) || isNaN(mois) || isNaN(annee)) return false;			
			if (jour<1 || jour>31) return false;
			if (mois<1 || mois>12) return false;
			if (annee< current.getFullYear()-120 || annee>current.getFullYear()) return false;
			if (jour > maxJours[mois]) return false;
			if (mois == 2 && jour == 29 && !isBissexYear(annee)) return false;	
			return true ;
		}
	
		
		public static function isBissexYear( year : Number ) : Boolean
		{
			var d0:Number = year / 4;
			var d1:Number = year / 100;
			var d2:Number = year / 400;
			return ((parseInt(d0.toString()) == d0 && parseInt(d1.toString()) != d1) || parseInt(d2.toString()) == d2) ;
		}   
		
	}

}